package com.itheima.web.servlet;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                //调用service去查询
        UserService service=new UserServiceImpl();
        ArrayList<User> users=service.findAll();

        //将集合添加到请求域中
          req.setAttribute("users",users);

          //转发到用户列表
        req.getRequestDispatcher("/userlist.jsp").forward(req,resp);

    }


    public void add(HttpServletRequest req,HttpServletResponse resp) {

        try {
            //获取所有请求的map集合
            Map<String, String[]> map = req.getParameterMap();

            //将集合封装user对象
            User u=new User();
            BeanUtils.populate(u,map);

            //service去添加
            UserService service=new UserServiceImpl();
            service.add(u);

            //重定向到用户列表
            resp.sendRedirect(req.getContextPath()+"/userServlet?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void delete(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        //获取要删除的id
        String delId = req.getParameter("delid");
        //让service去删
        UserService service=new UserServiceImpl();
        service.delete(delId);
        //重定向用户列表
        resp.sendRedirect(req.getContextPath()+"/userServlet?method=findAll");
    }

    public void findById(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        //获取要被修改的用户id
        String upid = req.getParameter("upid");
        //先让service去查
        UserService service=new UserServiceImpl();
        //将用户信息保存到请求域中
        User user=service.findById(upid);
        req.setAttribute("u",user);
        //转发至修改页面
        req.getRequestDispatcher("/update.jsp").forward(req,resp);
    }

    public void update(HttpServletRequest req,HttpServletResponse resp){
        try {
            //设置请求参数编码
            req.setCharacterEncoding("UTF-8");
            //获取所有请求参数集合
            Map<String, String[]> map = req.getParameterMap();
            //将map集合封装到user对象
            User user=new User();
            BeanUtils.populate(user,map);
            //让service去修改用户信息
            UserService service=new UserServiceImpl();
            service.update(user);
            //重定向到用户列表
            resp.sendRedirect(req.getContextPath()+"/userServlet?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
